﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Cashier_Model;
using Cashier_Service;
using Cashier.utils;
using Cashier_Utils;

namespace Cashier
{
    public partial class LoginForm : Form
    {
        public LoginForm()
        {
            InitializeComponent();
            if (!String.IsNullOrEmpty(Config.USERNAME))
            {
                textBox_username.Text = Config.USERNAME;
            }
            if (!String.IsNullOrEmpty(Config.PASSWORD))
            {
                textBox_password.Text = EncryptionUtil.DesDecode(Config.PASSWORD);
                checkBox_password.Checked = true;
            }
        }

        /// <summary>
        /// 登录事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void button_login_Click(object sender, EventArgs e)
        {
            String username = textBox_username.Text;
            String password = textBox_password.Text;

            if (String.IsNullOrEmpty(username) || username.Length < 4)
            {
                textBox_username.BackColor = Color.Yellow;
                textBox_username.Focus();
                return;
            }
            if (String.IsNullOrEmpty(password) || password.Length < 6)
            {
                textBox_password.BackColor = Color.Yellow;
                textBox_password.Focus();
                return;
            }

            Admin admin = new AdminService().Login(username, password);
            if (admin!=null && admin.Id > 0)
            {
                Config.SetUsername(textBox_username.Text.Trim());
                if (checkBox_password.Checked)
                    Config.SetPassword(EncryptionUtil.DesEncode(password));
                else
                    Config.SetPassword(String.Empty);
                Config.SaveXml();
                DataTable rolePurreviewtable = new RolePurviewService().GetRolePurviewTable(0, 0, admin.Roleid);
                if (rolePurreviewtable != null)
                    admin.RolePurviewtable = rolePurreviewtable;
                MainForm mainform = UserMainForm.GetUserMainForm(this, admin);
                //MainForm mainform = ManagerMainForm.GetManagerMainForm(this, admin);
                this.Hide();
                mainform.Show();
            }
            else
            {
                MessageBox.Show("用户名或密码错误！");
            }
        }

        /// <summary>
        /// 回车键事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Enter_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyValue == 13)
            {
                button_login.Focus();
                this.button_login_Click(sender, e);
            }
        }

        /// <summary>
        /// 文本框失去焦点事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void textBox_LostFocus(object sender, EventArgs e)
        {
            try
            {
                TextBox textbox = (TextBox)sender;
                if (textbox.BackColor == Color.Yellow)
                {
                    textbox.BackColor = SystemColors.Window;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}
